﻿using AiFrame.InterfaceLib.Data.Patterns;
using AiFrame.InterfaceLib.Data.Provider;

namespace AiFrame.Base.Core.Plugins
{
    /// <summary>
    /// Diese Klasse dient als Aufbewahrung aller Provider,
    /// die registriert wurden.
    /// Zugriff nur über statische Singleton-Methode, diese Klasse
    /// ist Thread-Safe.
    /// Quelle: http://msdn.microsoft.com/en-us/library/ms998558.aspx
    /// </summary>
    class ProviderRegistration: IDbProviderRegistration
    {
        private static volatile ProviderRegistration _instance;
        private static object syncRoot = new object();
        private ObservableCollection<IDbProvider> _providerCollec;
        private IDbProvider _activeProvider;
        
        /// <summary>
        /// Initialisierungs-Konstruktor.
        /// </summary>
        private ProviderRegistration()
        {
            _providerCollec = new ObservableCollection<IDbProvider>();
        }

        public static ProviderRegistration Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock(syncRoot)
                    {
                        if (_instance == null)
                            _instance = new ProviderRegistration();
                    }
                }
                return _instance;
            }
        }

        #region Implementation of IDbProviderRegistration

        public void RegisterProvider(IDbProvider dbProvider)
        {
            if (_providerCollec.IndexOf(dbProvider) == -1)
                _providerCollec.Add(dbProvider);
        }

        public void UnregisterProvider(IDbProvider dbProvider)
        {
            _providerCollec.Remove(dbProvider);
        }

        public IDbProvider ActiveProvider
        {
            get
            {
                return _activeProvider;
            }
            set
            {
                _activeProvider = value;
            }
        }

        public ObservableCollection<IDbProvider> Providers
        {
            get { return _providerCollec; }
        }

        #endregion
    }
}
